const puppeteer = require('puppeteer')
const url = 'https://s.weibo.com/'
const searchText = '崔永元'
const sleep = time => new Promise(resolve => setTimeout(resolve, time))
const asyncOn = (target, e, cb) => new Promise(resolve => { target.on(e, () => { resolve(); cb && cb() }) })


const handler = async () => {
    const browser = await puppeteer.launch({
        args: ["--no-sandbox"],
        dumpio: false
    })
    const page = await browser.newPage()
    await page.goto(url, {
        waitUntil: 'networkidle2'
    })
    await sleep(500)
    await page.waitForSelector('.search-input input')
    await page.type('.search-input input', searchText)
    await page.click('.searchbox button')
    await asyncOn(page, 'load', () => { console.log('async loaded') })
    await sleep(500)
    const result = await page.evaluate(() => {
        console.log(document.body.innerText)
        return document.body.innerText
    })
    browser.close()
    process.send(result)
    process.exit(0)
}

handler()